import 'dart:io';

import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:video_compress/video_compress.dart';

/// 压缩返回类型
class CompressMediaFile {
  /// 缩略图
  final File? thumbnail;

  /// 媒体文件
  final MediaInfo? video;

  CompressMediaFile({this.thumbnail, this.video});
}

/// 压缩类
class DuCompress {
  /// 压缩图片
  /// @param file 需要压缩的图片
  /// @param minWidth 压缩的最小宽度
  /// @param minHeight 压缩的最小高度
  static Future<File?> image(File file,
      {int minWidth = 1920, int minHeight = 1080}) async {
    var result = await FlutterImageCompress.compressAndGetFile(
      file.absolute.path, "${file.path}_temp.jpg",
      // 保存图片一些信息
      keepExif: true,
      // 图片压缩质量
      quality: 88,
      // 旋转
      // rotate: 180,
      format: CompressFormat.jpeg,
      minHeight: minHeight,
      minWidth: minWidth,
    );

    // return Future(() {
    //   if (result?.path == null) {
    //     return null;
    //   }
    //   return File(result?.path ?? "");
    // });
    if (result?.path == null) {
      return null;
    }
    return File(result?.path ?? "");
  }

  /// 压缩视频
  /// @param file 需要压缩的文件
  static Future<CompressMediaFile?> video(File file) async {
    var result = await Future.wait([
      // 视频压缩
      VideoCompress.compressVideo(
        // 视频路径
        file.path,
        // 压缩质量
        quality: VideoQuality.Res640x480Quality,
        // 是否删除原图
        deleteOrigin: false,
        // 是否包含音频
        includeAudio: true,
        // 码率/帧率
        frameRate: 25,
      ),
      // 缩略图
      VideoCompress.getFileThumbnail(
        file.path,
        quality: 80,
        // 截图的位置
        position: -1000,
      ),
    ]);

    return CompressMediaFile(
        video: result.first as MediaInfo, thumbnail: result[1] as File);
  }

  /// 清理视频压缩缓存
  static Future<bool?> clean() async {
    return await VideoCompress.deleteAllCache();
  }

  /// 取消
  static Future<void> cancel() async {
    await VideoCompress.cancelCompression();
  }

  

}
